<?php
namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\UserVali;
use App\Model\Backstage\CustomerFollowSourde;
use App\Model\Backstage\User;
use App\Tool\Error;
use Illuminate\Http\Request;
use Validator;

/**
 * Class UserController
 * @package App\Http\Controllers\Backstage
 * 登录控制器
 * date:2018-4-30
 * author:lxy
 */
class LoginController extends Controller
{
    protected $user;

    public function __construct()
    {
       $this->user = new User();
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 用户登录
     */
    public function index(Request $request)
    {
        $params = $request->all();
        $vali = UserVali::loginValidate($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->user->loginUser($params);
        if ($res === -4) {
            //账号不存在
            return $this->error(Error::USERNAME_NULL);
        }
        if ($res === -5) {
            //账号被禁用
            return $this->error(Error::ACCOUNT_DISABLE);
        }
        if ($res === -6) {
            //账号被删除
            return $this->error(Error::ACCOUNT_DEL);
        }
        if ($res === -1) {
            //公司还在审核中不能登录
            return $this->error(Error::AUDIT);
        }
        if ($res === -2) {
            //公司账号到期没有续费无法登录
            return $this->error(Error::NO_RENEWAL);
        }
        if ($res === -3) {
            //未知错误 请联系管理员
            return $this->error(Error::PLEASE_CONTACT_ADMIN);
        }
        if($res){
            return $this->success('ok',$res);
        }else{
            return $this->error(Error::PASSWORD_ERROR);
        }

    }


}